/**
 *
 * Copyright (C) 2008  Igor Vdovichenko
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Though a sincere effort has been made to deliver a professional,
 * quality product,the library itself is distributed WITHOUT ANY WARRANTY;
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 */
package com.vin.scriptutils.utils.ssh;

import com.vin.scriptutils.UString;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 *
 * @author igor
 */
public class ResultReader {

    private Charset charset;
    private final StringBuilder errorOutput = new StringBuilder();
    private final StringBuilder stdOutput = new StringBuilder();
    private Integer exitStatus;

    public ResultReader() {
        this(Charset.forName("UTF-8"));
    }

    public ResultReader(Charset charset) {
        this.charset = charset;
    }
    
    public Integer getExitStatus() {
        return this.exitStatus;
    }

    public String getErrorOutput() {
        return this.errorOutput.toString();
    }

    public String getStdOutput() {
        return this.stdOutput.toString();
    }   

    public boolean isError() {
        return ((null != getExitStatus() && 0 != getExitStatus().intValue()) || getErrorOutput().length() > 0);
    }

    
    public String[] getLinesStdOutput() {
        return this.stdOutput.toString().split(UString.UNIX_LINE_SEPARATOR);
    }

    public String getFirstLineStdOutput() {
        return (getLinesStdOutput() != null && getLinesStdOutput().length > 0) ? getLinesStdOutput()[0] : null;
    }

    public ResultReader processResult(SshTaskResult result) throws IOException {
        try {

            this.exitStatus = result.getExitStatus();

            InputStream resultOutputStream = result.getOutputStream();
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(resultOutputStream, this.charset));
                String line;
                while ((line = reader.readLine()) != null) {
                    stdOutput.append(line).append(UString.UNIX_LINE_SEPARATOR);
                }
            } finally {
                resultOutputStream.close();
            }
            InputStream resultErrorStream = result.getErrorStream();
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(resultErrorStream, this.charset));
                String line;
                while ((line = reader.readLine()) != null) {
                    errorOutput.append(line).append(UString.UNIX_LINE_SEPARATOR);
                }
            } finally {
                resultErrorStream.close();
            }
        } finally {
        }

        return this;

    }
}
